import {useTableActionRedux} from "@/components/ant-ext-table/redux/HAdminContext";
import {useActionForm,useEdit} from "@/common/hooks";
import { RequiredType, FieldType } from "@/const/const";
import HModal from "@/components/ant-ext/HModal";
import {Form} from "antd";
import HFormItem from "@/components/ant-ext/HFormItem";
import BankTemplate,{BankTemplateEntity} from "@/api/bank-template";
import ComminInterface from "@/api/common-interface";
import {util} from "@/common/util";
import {SelectElement} from "@/common/zct-types";
import {useState,useEffect} from "react";

export default function AdminEdit() {
    const [loading, setLoading] = useState(false)
    const [bankSelectList, setBankSelectList] = useState<SelectElement[]>([])
    const {action, reset} = useTableActionRedux<BankTemplateEntity>()
    const {form, key, title} = useActionForm(action, [BankTemplate.EDIT], BankTemplate.MAPPING)
    const close = () => {
        reset()
        form.resetFields()
    }
    const saveAndFlush = useEdit(key===BankTemplate.EDIT ? BankTemplate.doEdit : BankTemplate.doSave, {
        setLoading: setLoading,
        success: () => {
            reset()
        }
      })

      useEffect(() => {
            ComminInterface.bankList({
             setLoading, success: (data) => {
                const arr: never[] = []
                const bankListMapping = util.SelectBankMapping(arr, data)
                setBankSelectList(bankListMapping)
                }
            })
    }, [action, form])

    return <>
        <HModal onCancel={close} type={"setUp"} width={500} onOk={form.submit}
                confirmLoading={loading}
                visible={title !== undefined}
                title={title}
        >
            <Form name={"editForm"} form={form} labelCol={{span: 5}}
                  onFinish={() => {
                    saveAndFlush({ body: {...form.getFieldsValue(),targetType:"1"} })
                    
                  }}>
                <HFormItem label={"id"} name={"id"} hidden={key === BankTemplate.ADD}
                           disabled={key !== BankTemplate.ADD}/>
                <HFormItem label={"所属银行"} name={"targetId"} required type={"select"} dataSource={bankSelectList} />
                <HFormItem label={"字段类型"} name={"type"} type={"select"} dataSource={FieldType.select} required/>
                <HFormItem label={"字段名称"} name={"title"} required/>
                <HFormItem label={"字段英文名"} name={"keyCode"} required/>
                <HFormItem label={"是否必填"} type={"radio"} name={"required"} dataSource={RequiredType.select} required/>
                <HFormItem label={"排序"} name={"sortNum"} required/>
                <HFormItem label={"备注"} name={"decription"} required/>
                <HFormItem label={"code"} name={"dictCode"} required/>

            </Form>
        </HModal>
    </>
}